//
//  JumpView.swift
//  JumpAnimation
//
//  Created by tianqi on 2017/11/9.
//  Copyright © 2017年 david.com. All rights reserved.
//

import UIKit

private let jumpDuration = 0.2
private let downDuration = 0.3

class JumpView: UIView {
    
    @IBOutlet weak var shawView: UIImageView!
    @IBOutlet weak var starView: UIImageView!
    var _animationing = false
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        shawView.image = #imageLiteral(resourceName: "shadow_new")
        shawView.alpha = 0.4
    }
    
    var status:Bool = false {
        didSet {
            status == true ? (starView.image = #imageLiteral(resourceName: "icon_star_incell_red")) : (starView.image = #imageLiteral(resourceName: "blue_dot"))
        }
    }
    
    func startAnimation() {
        
        if _animationing {return}
        _animationing = true
        
        let rotationAm = CABasicAnimation(keyPath: "transform.rotation.y")
        rotationAm.fromValue = 0
        rotationAm.toValue = CGFloat.pi * 0.5
        rotationAm.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        
        let positionAm = CABasicAnimation(keyPath: "position.y")
        positionAm.fromValue = starView.center.y
        positionAm.toValue = starView.center.y - starView.bounds.height * 0.5
        positionAm.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        
        let groupAm = CAAnimationGroup()
        groupAm.duration = jumpDuration
        groupAm.isRemovedOnCompletion = false
        groupAm.fillMode = kCAFillModeForwards
        groupAm.delegate = self
        groupAm.animations = [rotationAm,positionAm]

        starView.layer.add(groupAm, forKey: "jump")
    }
}

extension JumpView: CAAnimationDelegate {
    
    func animationDidStart(_ anim: CAAnimation) {
        if anim == starView.layer.animation(forKey: "jump") {
            
            UIView.animate(withDuration: jumpDuration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.shawView.alpha = 0.2
                self.shawView.bounds = CGRect(x: 0, y: 0, width: self.shawView.bounds.width * 1.5, height: self.shawView.bounds.height)
            }, completion: { (_) in
                
            })
        }else if anim == starView.layer.animation(forKey: "down") {
            UIView.animate(withDuration: jumpDuration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.shawView.alpha = 0.4
                self.shawView.bounds = CGRect(x: 0, y: 0, width: self.shawView.bounds.width / 1.5, height: self.shawView.bounds.height)
            }, completion: { (_) in
                
            })
        }
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
        if anim == starView.layer.animation(forKey: "jump") {
            
            status = !status
            
            let rotationAm = CABasicAnimation(keyPath: "transform.rotation.y")
            rotationAm.fromValue = CGFloat.pi * 0.5
            rotationAm.toValue = CGFloat.pi
            rotationAm.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            
            let positionAm = CABasicAnimation(keyPath: "position.y")
            positionAm.fromValue = starView.center.y - starView.bounds.height * 0.5
            positionAm.toValue = starView.center.y
            positionAm.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            
            let groupAm = CAAnimationGroup()
            groupAm.duration = downDuration
            groupAm.isRemovedOnCompletion = false
            groupAm.fillMode = kCAFillModeForwards
            groupAm.delegate = self
            groupAm.animations = [rotationAm,positionAm]
            
            starView.layer.add(groupAm, forKey: "down")
            
        }else if anim == starView.layer.animation(forKey: "down") {
            _animationing = false
        }
    }
}




